package me.pearjelly.dao.hibernate;

import java.util.List;

import me.pearjelly.dao.PicGroupDao;
import me.pearjelly.model.PicGroup;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;

@Repository
public class PicGroupDaoImpl extends AbstractDao implements PicGroupDao {

	@Override
	public Long save(PicGroup picGroup) {
		try {
			return (Long) getHibernateTemplate().save(picGroup);
		} catch (DataAccessException e) {
			logger.error(e.getMessage());
			throw e;
		}
	}

	@Override
	public void update(PicGroup picGroup) {
		try {
			getHibernateTemplate().update(picGroup);
		} catch (DataAccessException e) {
			logger.error(e.getMessage());
			throw e;
		}
	}

	@Override
	public PicGroup findByRefer(String refer) {
		try {
			@SuppressWarnings("unchecked")
			List<PicGroup> picGroups = (List<PicGroup>) getHibernateTemplate()
					.find("from PicGroup pg where pg.refer=?", refer);
			return picGroups != null && picGroups.size() > 0 ? picGroups.get(0)
					: null;
		} catch (DataAccessException e) {
			logger.error(e.getMessage());
			throw e;
		}
	}

	@Override
	public PicGroup findById(Long id) {
		try {
			return (PicGroup) getHibernateTemplate().get(PicGroup.class, id);
		} catch (DataAccessException e) {
			logger.error(e.getMessage());
			throw e;
		}
	}

	private static final Logger logger = LoggerFactory
			.getLogger(PicGroupDaoImpl.class);
}
